import projectDir from 'fs-jetpack';

/**
 * 查看目录(层级显示)
 * @param  {string} path
 * @return {Promise}
 */
export async function dir(path = './raml') {
    const tree = await projectDir.inspectTreeAsync(path);
    return tree || {};
}

/**
 * 查看文件内容
 * @param  {string} path
 * @return {Object}
 */
export async function getFileContent(path) {
    const result = await projectDir.read(`raml/${path}`);
    return result;
}

/**
 * 创建文件或文件夹
 * @param  {string} type
 * @param  {string} path
 * @return {Object}
 */
export async function createFileOrFolder(type, path) {
    if (type === 'file') {
        return await projectDir.fileAsync(`raml/${path}`);
    } else {
        return await projectDir.dirAsync(`raml/${path}`);
    }
}

/**
 * 删除文件或文件夹
 * @param  {string} type
 * @param  {string} path
 * @return {Object}
 */
export async function delFileOrFolder(type, path) {
    return await projectDir.removeAsync(`raml/${path}`);
}

/**
 * 重命名文件或文件夹
 * @param  {string} path
 * @param  {string} newName
 * @return {Object}
 */
export async function renameFileOrFolder(path, newName) {
    return await projectDir.renameAsync(`raml/${path}`, newName);
}

/**
 * 保存文件
 * @param  {string} path
 * @param  {string} content
 * @return {Object}
 */
export async function saveFile(path, content) {
    return await projectDir.writeAsync(`raml/${path}`, content);
}
